//
//  YCLStatus.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/13.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLStatus: NSObject {
    
    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id:	Int64 = 0
    
    /// 微博信息内容
    var text: String? {
    
        didSet{
        
            attrText = (text?.emoticonNameTextToPicAttText())!
        }
    }
    
    //带图片的微博信息内容
    var attrText:NSAttributedString?
    
    
    /// 微博来源
    var source: String? {
    
        didSet{
        
            if let newSource = source {
                
                guard let firstRange = newSource.rangeOfString("\">") else {
                
                    YCLPrint("没有匹配")
                    return
                }
                
                guard let secondRange = newSource.rangeOfString("</a>") else {
                
                    YCLPrint("没有匹配")
                    return
                }
                
                let range = firstRange.endIndex..<secondRange.startIndex
                
                source = "来自" + newSource.substringWithRange(range)
            }
        }
    }
    
    /// 微博的配图 [Int], [String: AnyObject], [[String: AnyObject]]
    /// 数组,里面保存的是字典
    var pic_urls:[[String: AnyObject]]?{
    
        didSet{
            
            let count = pic_urls?.count ?? 0
            
            if count == 0 {
                
                return
            }
            
            //创建空的可变storePictureURLs数组
            storePictureURLs = [NSURL]()
            
            //遍历字典数组
            for dict in pic_urls! {
                
                //通过key：thumbnail_pic获得url字符串
                let urlString = dict["thumbnail_pic"] as? String
                let url = NSURL.init(string: urlString!)
                storePictureURLs?.append(url!)
            }

        }
    }
    
    //提供url数组
    var storePictureURLs:[NSURL]?
    
    
    //用户信息
    var user:YCLUser?
    
    //被转发微博
    var  retweeted_status: YCLStatus?
    
    
    
    //字典转模型
    init(dict:[String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //拦截给user属性字典转模型
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
            
            //继续字典转模型
            if let dict = value as? [String : AnyObject] {
            
                user = YCLUser.init(dict: dict)
            }
            return
        }
        
        if key == "retweeted_status" {
            
            //将字典继续转模型
            if let dict = value as? [String : AnyObject] {
                
                retweeted_status = YCLStatus.init(dict: dict)
            }
            return
        }
        
        //其他属性按照父类方法继续字典转模型
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //打印对象
    override var description: String {
    
        get{
        
            let keys = ["created_at", "id", "text", "source", "pic_urls","user"]
            
            return "\n \t微博模型: \(self.dictionaryWithValuesForKeys(keys).description)"
        }
    }
    

}
